/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package plar.Properties;

/**
 *
 * @author spok
 */
import java.awt.Point;
import java.awt.Rectangle;
import plar.*;
        
public class MoveElements extends Property{

   public MoveElements()
    {
     super("moveelements");
    }
   public boolean doElementsProperty(Element e1,Element e2)
   {
       return true;
   }
    public boolean doElementProperty(Element e)
    {
          
          Point speed = (Point) e.properties.get("speed");
          Rectangle from = e.getPosition();
          
          Point to = (Point) e.properties.get("to");
          if(to==null)
          {
              to=new Point(e.getPosition().x,e.getPosition().y);
              to.x+=speed.x;
              to.y+=speed.y;
          }
 
          int sumSpeed = speed.x+speed.y;
          Point moviment = new Point(from.x,from.y);
          for(int i=0;i<sumSpeed;i++)
          {
             Point lastMoviment = new Point(moviment);
             if(speed.x!=0 && speed.y!=0 )
             {
                            int r = (int) (Math.random() * sumSpeed);
                            if(r<=speed.x)
                            {
                                moviment.x++;
                            }else
                            {
                                moviment.y++;
                            }
                        }else if(speed.x!=0) 
                        {
                            moviment.x++;
                        }
                         else if(speed.y!=0) 
                         {
                             moviment.y++;
                         }
                        
                       e.setPosition(moviment);
                        if(level.callProperty("collisiondetect",e))
                           moviment=new Point(lastMoviment);
                       e.setPosition(moviment);

                    }
          return true;
    }
}